Security News
Internet Archive Hacked, 31 Million Record Compromised
The Internet Archive's "Wayback Machine" has been hacked and defaced, with 31 millions records compromised.
The jju npm package is a JSON utility library that provides functionalities for parsing, stringifying, and manipulating JSON data. It is designed to be fast and flexible, supporting various JSON formats and allowing for error-tolerant parsing.
Parsing JSON
This feature allows you to parse JSON strings into JavaScript objects. The jju.parse function can handle various JSON formats and is tolerant of errors.
const jju = require('jju');
const jsonString = '{"key": "value"}';
const parsedData = jju.parse(jsonString);
console.log(parsedData);
Stringifying JSON
This feature allows you to convert JavaScript objects into JSON strings. The jju.stringify function provides options for formatting the output.
const jju = require('jju');
const jsonObject = { key: 'value' };
const jsonString = jju.stringify(jsonObject);
console.log(jsonString);
Modifying JSON
This feature allows you to modify existing JSON strings. The jju.modify function can update specific keys without needing to parse and re-stringify the entire JSON.
const jju = require('jju');
const jsonString = '{"key": "value"}';
const modifiedString = jju.modify(jsonString, { key: 'newValue' });
console.log(modifiedString);
Error-tolerant Parsing
This feature allows you to parse JSON strings that may not strictly adhere to the JSON standard. The jju.parse function can handle common errors and still produce a valid JavaScript object.
const jju = require('jju');
const malformedJsonString = '{key: "value"}';
const parsedData = jju.parse(malformedJsonString, { mode: 'json' });
console.log(parsedData);
JSON5 is a JSON utility library that extends JSON syntax to make it more human-friendly. It allows for comments, trailing commas, and more. Compared to jju, JSON5 focuses on enhancing the JSON format itself, while jju provides more flexible parsing and modification capabilities.
fast-json-stringify is a library designed for high-performance JSON stringification. It uses JSON schema definitions to generate optimized code for converting JavaScript objects to JSON strings. While jju offers a balance of features for parsing and stringifying, fast-json-stringify is specialized for speed in stringification.
jsonlint is a JSON parser and validator that checks for syntax errors in JSON data. It is useful for ensuring JSON data is correctly formatted. Compared to jju, jsonlint is more focused on validation and error checking, whereas jju provides broader functionality for parsing, stringifying, and modifying JSON.
jju
- a set of utilities to work with JSON / JSON5 documents
yarn add jju
or
npm install jju
This module provides following functions:
All functions are able to work with a standard JSON documents. jju.parse()
and jju.stringify()
are better in some cases, but slower than native JSON.parse()
and JSON.stringify()
versions. Detailed description see below.
/*
* Main syntax:
*
* `text` - text to parse, type: String
* `options` - parser options, type: Object
*/
jju.parse(text[, options])
// compatibility syntax
jju.parse(text[, reviver])
Options:
"ignore" - ignore reserved keys
"throw" - throw SyntaxError in case of reserved keys
"replace" - replace reserved keys, this is the default JSON.parse behaviour, unsafe
Reserved keys are keys that exist in an empty object (hasOwnProperty
, __proto__
, etc.).
// 'ignore' will cause reserved keys to be ignored:
parse('{hasOwnProperty: 1}', {reserved_keys: 'ignore'}) == {}
parse('{hasOwnProperty: 1, x: 2}', {reserved_keys: 'ignore'}).hasOwnProperty('x') == true
// 'throw' will cause SyntaxError in these cases:
parse('{hasOwnProperty: 1}', {reserved_keys: 'throw'}) == SyntaxError
// 'replace' will replace reserved keys with new ones:
parse('{hasOwnProperty: 1}', {reserved_keys: 'replace'}) == {hasOwnProperty: 1}
parse('{hasOwnProperty: 1, x: 2}', {reserved_keys: 'replace'}).hasOwnProperty('x') == TypeError
null_prototype - create object as Object.create(null) instead of '{}' (Boolean)
if reserved_keys != 'replace'
, default is false
if reserved_keys == 'replace'
, default is true
It is usually unsafe and not recommended to change this option to false in the last case.
reviver - reviver function - Function
This function should follow JSON specification
mode - operation mode, set it to 'json' if you want to throw on non-strict json files (String)
/*
* Main syntax:
*
* `value` - value to serialize, type: *
* `options` - serializer options, type: Object
*/
jju.stringify(value[, options])
// compatibility syntax
jju.stringify(value[, replacer [, indent])
Options:
ascii - output ascii only (Boolean, default=false) If this option is enabled, output will not have any characters except of 0x20-0x7f.
indent - indentation (String, Number or Boolean, default='\t') This option follows JSON specification.
quote - enquoting char (String, "'" or '"', default="'")
quote_keys - whether keys quoting in objects is required or not (String, default=false)
If you want {"q": 1}
instead of {q: 1}
, set it to true.
sort_keys - sort all keys while stringifying (Boolean or Function, default=false)
By default sort order will depend on implementation, with v8 it's insertion order. If set to true
, all keys (but not arrays) will be sorted alphabetically. You can provide your own sorting function as well.
replacer - replacer function or array (Function or Array) This option follows JSON specification.
no_trailing_comma = don't output trailing comma (Boolean, default=false)
If this option is set, arrays like this [1,2,3,]
will never be generated. Otherwise they may be generated for pretty printing.
mode - operation mode, set it to 'json' if you want correct json in the output (String)
Currently it's either 'json' or something else. If it is 'json', following options are implied:
/*
* Main syntax:
*
* `text` - text to tokenize, type: String
* `options` - parser options, type: Object
*/
jju.tokenize(text[, options])
Options are the same as for the jju.parse
function.
Return value is an array of tokens, where each token is an object:
whitespace
, comment
, key
, literal
, separator
or newline
key
or literal
You can check tokenizer for yourself using this demo.
/*
* Main syntax:
*
* `text` - text to analyze, type: String
* `options` - parser options, type: Object
*/
jju.analyze(text[, options])
Options are the same as for the jju.parse
function.
Return value is an object defining a programming style in which the document was written.
"
or '
depending on which quote is preferredtrue
if unquoted keys were used at least oncetrue
if input has a whitespace tokentrue
if input has a comment tokentrue
if input has a newline tokentrue
if input has at least one trailing comma/*
* Main syntax:
*
* `text` - original text, type: String
* `new_value` - new value you want to set
* `options` - parser or stringifier options, type: Object
*/
jju.update(text, new_value[, options])
If you want to update a JSON document, here is the general approach:
// here is your original JSON document:
var input = '{"foo": "bar", "baz": 123}'
// you need to parse it first:
var json = jju.parse(input, {mode: 'json'})
// json is { foo: 'bar', baz: 123 }
// then you can change it as you like:
json.foo = 'quux'
json.hello = 'world'
// then you run an update function to change the original json:
var output = jju.update(input, json, {mode: 'json'})
// output is '{"foo": "quux", "baz": 123, "hello": "world"}'
Look at this demo to test various types of json.
In a few cases it makes sense to use this module instead of built-in JSON methods.
Parser:
In case of syntax error, JSON.parse does not return any good information to the user. This module does:
$ node -e 'require("jju").parse("[1,1,1,1,invalid]")'
SyntaxError: Unexpected token 'i' at 0:9
[1,1,1,1,invalid]
^
This module is about 5 times slower, so if user experience matters to you more than performance, use this module. If you're working with a lot of machine-generated data, use JSON.parse instead.
Stringifier:
This module behaves more smart when dealing with object and arrays, and does not always print newlines in them:
$ node -e 'console.log(require("./").stringify([[,,,],,,[,,,,]], {mode:"json"}))'
[
[null, null, null],
null,
null,
[null, null, null, null]
]
JSON.stringify will split this into 15 lines, and it's hard to read.
Yet again, this feature comes with a performance hit, so if user experience matters to you more than performance, use this module. If your JSON will be consumed by machines, use JSON.stringify instead.
As a rule of thumb, if you use "space" argument to indent your JSON, you'd better use this module instead.
FAQs
a set of utilities to work with JSON / JSON5 documents
The npm package jju receives a total of 2,719,391 weekly downloads. As such, jju popularity was classified as popular.
We found that jju demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Internet Archive's "Wayback Machine" has been hacked and defaced, with 31 millions records compromised.
Security News
TC39 is meeting in Tokyo this week and they have approved nearly a dozen proposals to advance to the next stages.
Security News
Our threat research team breaks down two malicious npm packages designed to exploit developer trust, steal your data, and destroy data on your machine.